const storage = window.localStorage

/**
 * 向localStorage中添加键值对
 * @param {string} key
 * @param {!Object} value
 */
export function save (key, value) {
  storage.setItem(key, value)
}

/**
 * 向localStorage中添加多个键值对
 * @param {!Object} datas
 */
export function saveMulti (datas) {
  datas.forEach(data => save(data.key, data.value))
}

/**
 * 根据key读取value
 * @param {string} key 指定的key
 * @return {Object} 对应的value
 */
export function read (key) {
  return storage.getItem(key)
}

/**
 * 根据指定的多个key读取value
 * @param {Array.<string>} keys 指定的多个key
 * @return {Array} value的数组
 */
export function readMulti (keys) {
  return keys.map(key => read(key))
}

/**
 * 删除键值对
 * @param {string} key 指定key
 * @param {string=} clearAll 是否需要删除所有内容，默认为false
 */
export function clear (key, clearAll = false) {
  if (clearAll) {
    storage.clear()
  } else {
    storage.removeItem(key)
  }
}

/**
 * 删除多个指定的键值对
 * @param {Array.<string>} keys
 */
export function clearMulti (keys) {
  keys.forEach(key => clear(key))
}
